package br.edu.ifrn.view;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpSession;

import br.edu.ifrn.model.Documento;

@ManagedBean
@ViewScoped
public class DetalheMBean {
	private Documento documento;
	
	public DetalheMBean() {
		HttpSession session = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
		documento = (Documento) session.getAttribute("documento");
		
		String path = ((ServletContext)FacesContext.getCurrentInstance().
				getExternalContext().getContext()).getRealPath("/") + "tmp/";
		FileOutputStream os;
		try {
			os = new FileOutputStream(path + documento.getArquivo().getNome());
			os.write(documento.getArquivo().getConteudo());
			os.flush();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}	
	}
	
	public Documento getDocumento() {
		return documento;
	}
	
	public boolean isPDFOrImage() {
		return documento.getArquivo().isPDFOrImage();
	}
}
